#include "spleef.h"

Spleef::Spleef()
{
	this->phase = 0; this->next_phase = 0;
	this->tick = SDL_GetTicks();
	this->active = false;
	this->x = 76; this->y = 100; this->z = 50;
	this->w = this->d = 15; this->h = 6;
}

void Spleef::update(Server* server)
{
	if (this->active) {
		Spleef* s = this;
		if (s->phase == 0 && this->next_phase == 0) {
			server->set_box(s->x,  s->y,  s->z,  s->w +s->x,   s->y,  s->z +s->d, BLOCK_PINK);
		} else if (this->next_phase == 1 && this->phase == 0) {
			int x, y, z;
			x = (s->x + s->w/2)*32;
			y = (s->y + 2) *32;
			z = (s->z + s->d/2)*32;
			for (std::set<char>::iterator i = this->players.begin(); i != this->players.end(); i++) {
				if (server->is_connected(*i) == 0)
					this->players.erase(*i);
				else {
					server->move_player(x,y,z,0,0,*i);
				}
			}
			this->phase = 1;
			this->tick = SDL_GetTicks();
		} else if (this->next_phase == 1 && this->phase == 1) {
			int x, y, z;
			x = (s->x + s->w/2)*32;
			y = (s->y + 2) *32;
			z = (s->z + s->d/2)*32;
			server->set_box(s->x,  s->y,  s->z,  s->w +s->x,   s->y,  s->z +s->d, BLOCK_PINK);
			std::set<char> temp;
			server->find_players(s->x-1,  s->y,  s->z-1,  s->w +s->x,   s->y + 6,  s->z +s->d, &temp);
			for (std::set<char>::iterator i =this->players.begin(); i !=this->players.end(); i++) {
				if (temp.count(*i) == 0) server->move_player(x,y,z,0,0,*i);
			}
			static int countdown = 10;
			if (countdown == 10) {
				server->send_chat_all(255, "Game starting in 10 seconds!");
				countdown -= 5;
			} else if (countdown == 5 && SDL_GetTicks() - this->tick > 5000) {
				server->send_chat_all(255, "5");
				countdown -= 1;
			} else if (countdown == 4 && SDL_GetTicks() - this->tick > 6000) {
				server->send_chat_all(255, "4");
				countdown -= 1;
			} else if (countdown == 3 && SDL_GetTicks() - this->tick > 7000) {
				server->send_chat_all(255, "3");
				countdown -= 1;
			} else if (countdown == 2 && SDL_GetTicks() - this->tick > 8000) {
				server->send_chat_all(255, "2");
				countdown -= 1;
			} else if (countdown == 1 && SDL_GetTicks() - this->tick > 9000) {
				server->send_chat_all(255, "1");
				countdown -= 1;
			} else if (countdown == 0 && SDL_GetTicks() - this->tick > 10000) {
				server->send_chat_all(255, "GO!");
				countdown = 10;
				this->phase = 2;
			}
		} else if (this->phase == 2) {
			std::set<char> temp;
			server->find_players(s->x,  s->y,  s->z,  s->w +s->x,   s->y + 6,  s->z +s->d, &temp);
			for (std::set<char>::iterator i =this->players.begin(); i !=this->players.end(); i++) {
				if( temp.count(*i) == 0) {
					char tempChar[64];
					memset(tempChar, 0, sizeof(tempChar));
					strcpy(tempChar, server->get_player_name(*i).c_str());
					strcat(tempChar, " was eliminated!");						
					server->send_chat_all(255, tempChar);
					this->players.erase(i);
				}
			}
			if (this->players.size() == 1) {
				char tempChar[64];
				memset(tempChar, 0, sizeof(tempChar));
				strcpy(tempChar, server->get_player_name(*this->players.begin()).c_str());
				strcat(tempChar, " IS VICTORIOUS!");						
				server->send_chat_all(255, tempChar);
				this->phase = 0;
				this->active = false;
				this->next_phase = 0;
			}
		}
	}
}

void Spleef::set_phase(int p)
{
	if (p == 0 && !this->active && this->phase == 0 && this->next_phase == 0) this->active = true;
	else if (p == 1 && this->active && this->phase == 0 && this->next_phase == 0) this->next_phase = 1;
}

void Spleef::add_player(int id)
{
	this->players.insert(id);
}

bool Spleef::can_join()
{
	return (this->active && this->phase == 0 && this->next_phase == 0);
}
